package cn.com.gaobb.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @package: cn.yungtay.shelf.utils
 * @description: 对对象的“”和null进行判断的工具类。
 * @author: haopt
 * @date: 2020 0810
 **/
@Slf4j
public class DataUtil {

    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        }
        if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        }
        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (Object o : object) {
                if (!isNullOrEmpty(o)) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

    public static String getString(Map<String, Object> params, String key) {
        if (DataUtil.isNullOrEmpty(params.get(key))) {
            return "";
        }
        String s = params.get(key).toString();
        try {
            Double d = Double.parseDouble(s);
            if (d == d.intValue()) {
                return d.intValue() + "";
            }
            return new BigDecimal(s).toPlainString();
        } catch (Exception e) {
            return s;
        }
    }

    public static Date parse(String dateString){
        Date date = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年 MM月dd日 hh:mm");
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            log.debug("输入时间格式有误，请重新输入");
        }
        return date;
    }
    public static Map<String, String> format (Date date){
        Map<String, String> map = new HashMap<>();
        try {
            DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,Locale.CHINA); //显示日期,上下午，时间（精确到秒）
            String s = df5.format(date);
            String[] s1 = s.split(" ");
            map.put("year", s1[0]);
            map.put("period", s1[1].substring(0, 2));
            map.put("hf", s1[1].substring(2,8));
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("输入时间格式有误，请重新输入");
        }
        return map;
    }

    public static String getAppointmentMonth (Date date){
        String appointmentMonth = "";
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            appointmentMonth = cal.get(Calendar.MONTH) + "月";
            appointmentMonth = appointmentMonth + cal.get(Calendar.DATE) + "日 ";
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("输入时间格式有误，请重新输入");
        }
        return appointmentMonth;
    }
}
